<template>
  <div class="lost-found">
    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 左侧内容 -->
      <div class="content-left">
        <div class="page-header card">
          <h2>失物招领</h2>
          <p>找回你丢失的物品，或者帮助他人找到失物</p>
          
          <div class="header-actions">
            <el-button type="primary" @click="showPublishDialog">
              <el-icon><Plus /></el-icon>
              发布信息
            </el-button>
          </div>
        </div>

        <!-- 筛选器 -->
        <div class="filters card">
          <div class="filter-row">
            <el-radio-group v-model="filters.type" @change="handleFilterChange">
              <el-radio-button label="">全部</el-radio-button>
              <el-radio-button label="lost">丢失</el-radio-button>
              <el-radio-button label="found">拾到</el-radio-button>
            </el-radio-group>
            
            <el-select v-model="filters.status" placeholder="状态" @change="handleFilterChange">
              <el-option label="全部" value="" />
              <el-option label="进行中" value="open" />
              <el-option label="已解决" value="closed" />
            </el-select>
            
            <el-input
              v-model="filters.keyword"
              placeholder="搜索物品..."
              style="width: 300px"
              @keyup.enter="handleFilterChange"
            >
              <template #append>
                <el-button icon="Search" @click="handleFilterChange" />
              </template>
            </el-input>
          </div>
        </div>

        <!-- 失物列表 -->
    <div class="lost-items">
      <el-skeleton :loading="loading" animated>
        <template #template>
          <div v-for="n in 5" :key="n" class="skeleton-item card">
            <el-skeleton-item variant="image" style="width: 120px; height: 120px" />
            <div style="margin-left: 20px; flex: 1">
              <el-skeleton-item variant="h3" style="width: 50%" />
              <el-skeleton-item variant="text" style="width: 80%; margin-top: 8px" />
              <el-skeleton-item variant="text" style="width: 60%; margin-top: 8px" />
            </div>
          </div>
        </template>

        <template #default>
          <div v-if="items.length === 0" class="empty-state">
            <el-empty description="暂无失物信息" />
          </div>
          
          <div v-else class="item-list">
            <div v-for="item in items" :key="item.id" class="lost-item card">
              <div class="item-image">
                <el-image
                  v-if="item.images && item.images.length > 0"
                  :src="item.images[0]"
                  fit="cover"
                  style="width: 120px; height: 120px"
                />
                <div v-else class="no-image">
                  <el-icon size="32"><Picture /></el-icon>
                  <span>暂无图片</span>
                </div>
              </div>
              
              <div class="item-content">
                <div class="item-header">
                  <div class="item-tags">
                    <el-tag :type="item.type === 'lost' ? 'danger' : 'success'">
                      {{ item.type === 'lost' ? '丢失' : '拾到' }}
                    </el-tag>
                    <el-tag :type="item.status === 'open' ? 'warning' : 'info'">
                      {{ item.status === 'open' ? '进行中' : '已解决' }}
                    </el-tag>
                  </div>
                  <div class="item-time">{{ formatTime(item.createTime) }}</div>
                </div>
                
                <h3 class="item-title">{{ item.title }}</h3>
                <p class="item-description">{{ item.description }}</p>
                
                <div class="item-info">
                  <div class="info-item">
                    <el-icon><MapPin /></el-icon>
                    <span>{{ item.location }}</span>
                  </div>
                  <div class="info-item">
                    <el-icon><User /></el-icon>
                    <span>{{ item.user.username }}</span>
                  </div>
                </div>
                
                <div class="item-actions">
                  <el-button type="primary" size="small" @click="showContactDialog(item)">
                    联系TA
                  </el-button>
                  <el-button
                    v-if="item.user.id === currentUser?.id"
                    type="success"
                    size="small"
                    :disabled="item.status === 'closed'"
                    @click="updateStatus(item)"
                  >
                    标记已解决
                  </el-button>
                </div>
              </div>
            </div>
          </div>
        </template>
      </el-skeleton>

      <!-- 分页 -->
      <div v-if="total > 0" class="pagination">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>

    <!-- 发布对话框 -->
    <el-dialog v-model="publishDialogVisible" title="发布失物信息" width="600px">
      <el-form ref="publishFormRef" :model="publishForm" :rules="publishRules" label-width="80px">
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="publishForm.type">
            <el-radio label="lost">我丢失了物品</el-radio>
            <el-radio label="found">我拾到了物品</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item label="标题" prop="title">
          <el-input v-model="publishForm.title" placeholder="简要描述物品" />
        </el-form-item>
        
        <el-form-item label="详细描述" prop="description">
          <el-input
            v-model="publishForm.description"
            type="textarea"
            :rows="4"
            placeholder="详细描述物品特征、丢失/拾到的时间地点等"
          />
        </el-form-item>
        
        <el-form-item label="地点" prop="location">
          <el-input v-model="publishForm.location" placeholder="丢失/拾到的地点" />
        </el-form-item>
        
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="publishForm.contactPhone" placeholder="联系电话" />
        </el-form-item>
        
        <el-form-item label="QQ号">
          <el-input v-model="publishForm.contactQQ" placeholder="QQ号（可选）" />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="publishDialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="publishLoading" @click="handlePublish">
          发布
        </el-button>
      </template>
    </el-dialog>

    <!-- 联系信息对话框 -->
    <el-dialog v-model="contactDialogVisible" title="联系信息" width="400px">
      <div v-if="selectedItem" class="contact-info">
        <div class="contact-item">
          <label>联系人：</label>
          <span>{{ selectedItem.user.username }}</span>
        </div>
        <div class="contact-item">
          <label>电话：</label>
          <span>{{ selectedItem.contactPhone }}</span>
        </div>
        <div v-if="selectedItem.contactQQ" class="contact-item">
          <label>QQ：</label>
          <span>{{ selectedItem.contactQQ }}</span>
        </div>
      </div>
    </el-dialog>
  </div>
</div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'

const userStore = useUserStore()
const currentUser = userStore.userInfo

const loading = ref(false)
const items = ref([])
const total = ref(0)
const publishDialogVisible = ref(false)
const contactDialogVisible = ref(false)
const selectedItem = ref(null)
const publishLoading = ref(false)
const publishFormRef = ref()

// 筛选条件
const filters = reactive({
  type: '',
  status: '',
  keyword: ''
})

// 分页
const pagination = reactive({
  page: 1,
  size: 10
})

// 发布表单
const publishForm = reactive({
  type: 'lost',
  title: '',
  description: '',
  location: '',
  contactPhone: '',
  contactQQ: ''
})

// 发布表单验证规则
const publishRules = {
  type: [{ required: true, message: '请选择类型', trigger: 'change' }],
  title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
  description: [{ required: true, message: '请输入详细描述', trigger: 'blur' }],
  location: [{ required: true, message: '请输入地点', trigger: 'blur' }],
  contactPhone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ]
}

// 获取失物列表
const fetchItems = async () => {
  loading.value = true
  try {
    // 模拟数据
    const mockData = {
      records: [
        {
          id: 1,
          title: '丢失黑色钱包',
          description: '黑色皮质钱包，里面有身份证和银行卡',
          type: 'lost',
          status: 'open',
          location: '图书馆三楼',
          contactPhone: '13800138000',
          contactQQ: '123456789',
          images: ['https://picsum.photos/300/300?random=1'],
          user: { id: 1, username: '张同学' },
          createTime: '2024-01-01T10:00:00Z'
        },
        {
          id: 2,
          title: '拾到蓝色保温杯',
          description: '蓝色不锈钢保温杯，杯身有卡通贴纸',
          type: 'found',
          status: 'open',
          location: '食堂二楼',
          contactPhone: '13900139000',
          images: ['https://picsum.photos/300/300?random=2'],
          user: { id: 2, username: '李同学' },
          createTime: '2024-01-01T14:30:00Z'
        }
      ],
      total: 2
    }
    
    items.value = mockData.records
    total.value = mockData.total
  } catch (error) {
    console.error('获取失物列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 格式化时间
const formatTime = (time) => {
  return dayjs(time).fromNow()
}

// 筛选改变
const handleFilterChange = () => {
  pagination.page = 1
  fetchItems()
}

// 分页处理
const handleSizeChange = (size) => {
  pagination.size = size
  pagination.page = 1
  fetchItems()
}

const handleCurrentChange = (page) => {
  pagination.page = page
  fetchItems()
}

// 显示发布对话框
const showPublishDialog = () => {
  if (!userStore.isLoggedIn) {
    ElMessage.warning('请先登录')
    return
  }
  publishDialogVisible.value = true
}

// 显示联系信息对话框
const showContactDialog = (item) => {
  selectedItem.value = item
  contactDialogVisible.value = true
}

// 发布失物信息
const handlePublish = async () => {
  if (!publishFormRef.value) return
  
  try {
    await publishFormRef.value.validate()
    publishLoading.value = true
    
    // 这里调用API
    ElMessage.success('发布成功')
    publishDialogVisible.value = false
    
    // 重置表单
    Object.assign(publishForm, {
      type: 'lost',
      title: '',
      description: '',
      location: '',
      contactPhone: '',
      contactQQ: ''
    })
    
    fetchItems()
  } catch (error) {
    console.error('发布失败:', error)
  } finally {
    publishLoading.value = false
  }
}

// 更新状态
const updateStatus = async (item) => {
  try {
    // 这里调用API
    item.status = 'closed'
    ElMessage.success('已标记为已解决')
  } catch (error) {
    console.error('更新状态失败:', error)
  }
}

onMounted(() => {
  fetchItems()
})
</script>

<style scoped>
.page-header {
  text-align: center;
  padding: 48px;
  position: relative;
  background: rgba(255, 255, 255, 0.98);
  border: 1px solid rgba(226, 232, 240, 0.6);
  overflow: hidden;
}

.page-header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
}

.page-header h2 {
  font-size: 32px;
  font-weight: 700;
  color: #1e293b;
  margin-bottom: 12px;
  letter-spacing: -1px;
}

.page-header p {
  font-size: 18px;
  color: #64748b;
  margin-bottom: 24px;
  line-height: 1.6;
}

.header-actions {
  display: flex;
  justify-content: center;
  gap: 16px;
}

.filters {
  margin-bottom: 32px;
  background: rgba(255, 255, 255, 0.98);
  border: 1px solid rgba(226, 232, 240, 0.6);
}

.filter-row {
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
}

.filter-row .el-radio-group {
  background: rgba(248, 250, 252, 0.9);
  border-radius: 12px;
  padding: 4px;
}

.skeleton-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 24px;
  background: rgba(255, 255, 255, 0.98);
  border-radius: 20px;
  padding: 24px;
  border: 1px solid rgba(226, 232, 240, 0.6);
}

.lost-item {
  display: flex;
  align-items: flex-start;
  gap: 20px;
  margin-bottom: 20px;
  cursor: default;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  background: rgba(255, 255, 255, 0.98);
  border: 1px solid rgba(226, 232, 240, 0.6);
  position: relative;
  overflow: hidden;
}

.lost-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 4px;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  transform: scaleY(0);
  transition: transform 0.3s ease;
}

.lost-item:hover::before {
  transform: scaleY(1);
}

.lost-item:hover {
  transform: translateY(-6px);
  box-shadow: 
    0 20px 40px rgba(0, 0, 0, 0.1),
    0 8px 16px rgba(0, 0, 0, 0.06);
}

.item-image {
  flex-shrink: 0;
}

.item-image .el-image {
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.no-image {
  width: 120px;
  height: 120px;
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #94a3b8;
  border: 2px dashed #cbd5e1;
  font-size: 12px;
}

.no-image .el-icon {
  margin-bottom: 8px;
}

.item-content {
  flex: 1;
  min-width: 0;
}

.item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.item-tags {
  display: flex;
  gap: 8px;
}

.item-time {
  color: #64748b;
  font-size: 13px;
  font-weight: 500;
}

.item-title {
  color: #1e293b;
  margin-bottom: 12px;
  font-size: 18px;
  font-weight: 600;
  line-height: 1.4;
  letter-spacing: -0.3px;
}

.item-description {
  color: #475569;
  line-height: 1.7;
  margin-bottom: 16px;
  font-size: 15px;
}

.item-info {
  display: flex;
  gap: 32px;
  margin-bottom: 20px;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #64748b;
  font-size: 14px;
  font-weight: 500;
}

.info-item .el-icon {
  color: #667eea;
  font-size: 16px;
}

.item-actions {
  display: flex;
  gap: 12px;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}

.contact-info {
  padding: 20px;
}

.contact-item {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  font-size: 15px;
  padding: 12px;
  background: rgba(248, 250, 252, 0.9);
  border-radius: 12px;
  transition: all 0.3s ease;
}

.contact-item:hover {
  background: rgba(102, 126, 234, 0.08);
  transform: translateX(4px);
}

.contact-item label {
  width: 80px;
  color: #64748b;
  font-weight: 600;
}

.contact-item span {
  color: #1e293b;
  font-weight: 500;
}

.empty-state {
  text-align: center;
  padding: 80px 20px;
  background: rgba(255, 255, 255, 0.98);
  border-radius: 20px;
  border: 1px solid rgba(226, 232, 240, 0.6);
}

/* 主要内容区域布局 */
.main-content {
  display: flex;
  gap: 0;
  align-items: flex-start;
}

.content-left {
  flex: 1;
  min-width: 0;
}

@media (max-width: 768px) {
  .page-header {
    padding: 32px 24px;
  }
  
  .page-header h2 {
    font-size: 24px;
  }
  
  .page-header p {
    font-size: 16px;
  }
  
  .filter-row {
    flex-direction: column;
    align-items: stretch;
    gap: 16px;
  }
  
  .lost-item {
    flex-direction: column;
    gap: 16px;
  }
  
  .item-info {
    gap: 20px;
  }
  
  .item-actions {
    flex-wrap: wrap;
  }
}
</style>